print() function: The most fundamental way to display output on the console. It accepts one or more arguments (variables, strings, etc.) and adds a newline by default.
Printing Multiple Outputs in a Single Line:
Comma-Separated Arguments: Separate elements with commas (,) in the print() function to display them on the same line with a space in between.
Printing Multiple Outputs in a Single Line print
name = "Bob"
age = 25
city = "New York"
print(name, age, city)
Output
Bob 25 New York
end Parameter in print(): By default, print() inserts a newline after each argument. Set the end parameter to an empty string ("") to suppress the newline and keep printing on the same line.
Printing value on output by giving parameter at end
subject = "Mathematics"
score = 90
print("Your", subject, "score is:", score)
Output
Your Mathematics score is: 90
String Concatenation: Concatenate strings and variables using the + operator before printing to create a single output.
Concatenate strings and variables inside print function
product = "Laptop"
price = 799.99
print("The " + product + " costs $" + str(price))
Output
The Laptop costs $799.99
Direct Printing with variables
printing value directly within the print() function
radius = 5
area = 3.14159 * radius**2
print("The area of the circle is:", area)
Output
The area of the circle is: 78.53975
Escape Sequences
Escape sequences are special character combinations in Python strings that represent control characters or special actions. Here are some common examples:
\n: Newline (inserts a new line break)
\t: Horizontal tab (creates a tab space)
\\: Backslash (prints a backslash character)
\": Double quote character (prints a double quote)
\': Single quote character (prints a single quote)
Python escape sequence in printing output
print("Welcome to Tutorialsbox.com\nThis is python course.")
print("Welcome\tto our program!")
print("10\\5=2")
print("This is a \"Hello, world!\" program")
print('Always don\'t waste your time, it\'s precious!')
Output
Welcome to Tutorialsbox.com
This is python course.
Welcome to our program!
10\5=2
This is a "Hello, world!" program
Always don't waste your time, it's precious!
Formatted String Literals (f-strings): Introduced in Python 3.6, f-strings offer a concise and readable way to embed expressions directly within strings.
python output printing using formatted string literals
price = 19.99
discount = 0.10
final_price = price * (1 - discount)
print(f"The final price after a {discount*100}% discount is ${final_price:.2f}.")
Output
The final price after a 10.0% discount is $17.99.
Using variables directly in print using f-strings
name = "Sathish"
age = 25
print("Hello,", name, "!")
print(f"You are {age} years old.")
Output
Hello, Sathish !
You are 25 years old.
f-strings for Calculations: Utilize f-strings to embed calculations and variables seamlessly within the output.
calculation in print statement in f-strings
length = 10
width = 6
print(f"The perimeter of the rectangle is {2 * (length + width)}.")
Output
The perimeter of the rectangle is 32.
str.format() method: A versatile method for string formatting, especially useful when dealing with more complex formatting requirements.
python output using str.format() method example
course_name = "Python programming"
resource = "Tutorialsbox.com"
print("Learn {} easily from {}! \n Practice code using online {} compiler".format(course_name, resource, "Python"))
Output
Learn Python programming easily from Tutorialsbox.com!
Practice code using online Python compiler
Older Formatting (% operator): While less common nowadays, the % operator can still be used for formatting, but it's generally considered less readable than f-strings or str.format().
Python output formatting using % operator basic example
quantity = 10
item_price = 5.00
total_cost = quantity * item_price
print("Total cost: $%.2f" % total_cost)
Output
Total cost: $50.00